using System;
using System.Collections.Generic;
using System.Text;

using System.ComponentModel;
using System.Drawing;

namespace Athena.Objects
{
    internal class RectangleFConverter : ExpandableObjectConverter
    {
        public override bool CanConvertFrom(
        ITypeDescriptorContext context, Type t)
        {

            if (t == typeof(string))
            {
                return true;
            }
            return base.CanConvertFrom(context, t);
        }

        public override object ConvertFrom(
        ITypeDescriptorContext context,
        System.Globalization.CultureInfo info,
        object value)
        {

            if (value is string)
            {
                try
                {
                    string s = (string)value;
                    // parse the format "xPos, yPos, width, height"
                    //

                    string[] array = s.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                    if (array.Length == 4)
                    {
                        float xPos = Convert.ToSingle(array[0]);
                        float yPos = Convert.ToSingle(array[1]);
                        float width = Convert.ToSingle(array[2]);
                        float height = Convert.ToSingle(array[3]);

                        return new RectangleF(xPos, yPos, width, height);
                    }
                }
                catch { }
                // if we got this far, complain that we
                // couldn't parse the string
                //
                throw new ArgumentException(
                "Can not convert '" + (string)value +
                "' to type RectangleF");

            }
            return base.ConvertFrom(context, info, value);
        }

        public override object ConvertTo(ITypeDescriptorContext context,
        System.Globalization.CultureInfo culture,
        object value,
        Type destType)
        {
            if (destType == typeof(string) && value is RectangleF)
            {
                RectangleF temp = (RectangleF)value;
                return String.Format("{0}, {1}, {2}, {3}", temp.X, temp.Y, temp.Width, temp.Height);
            }
            return base.ConvertTo(context, culture, value, destType);
        }
    }
}
